package com.booksystem.campuslibrarymanagementsystem.controller.approve;

import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookSearchrequest.Booklist;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookSearchrequest.Booksearch;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookSearchrequest.BookselectR;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.AccountTable;
import com.booksystem.campuslibrarymanagementsystem.entity.BooksTable;
import com.booksystem.campuslibrarymanagementsystem.entity.CategoriesTable;
import com.booksystem.campuslibrarymanagementsystem.service.AccountTableService;
import com.booksystem.campuslibrarymanagementsystem.service.BooksTableService;
import com.booksystem.campuslibrarymanagementsystem.service.CategoriesTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/approval")
@Api(tags = "图书查询")
@RestController
public class BookQueryController {

    BooksTable booksTable = new BooksTable();
    @Autowired
    BooksTableService booksTableService;
    @Autowired
    AccountTableService accountTableService;
    @Autowired
    CategoriesTableService categoriesTableService;

//    /**
//     *
//     * @param  图书搜索，模糊查询
//     * 1,检查至少有一个搜索要求，没，提示请输入搜索条件
//     * 2，单个条件搜索
//     * 3，两个条件搜索
//     *4，三个条件搜索
//     *     获得图书类别到类别表，得到图书id,再通过图书id在图书表获得图书信息
//     * @return
//     */
    @PostMapping("/bookSelects")
    @ApiOperation(value = "图书搜索", notes = "通过模糊查询根据图书名称，作者，类别查询图书")
    public Result<List<BookselectR>> bookSelect(@RequestBody Booksearch bookselect) {
        Map<String, Object> parsed = JwtUtil.tokenPre(bookselect.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("approval")) {
                AccountTable accountTable = accountTableService.queryByAccount((String) account);
                if (accountTable != null) {
                    if (bookselect.getCategory().isEmpty()) {
                        List<BooksTable> list = booksTableService.selectbookList(bookselect.getTitle(), bookselect.getAuthor(), bookselect.getCategory());
                        if (!list.isEmpty()) {
                            ArrayList<BookselectR> list1 = new ArrayList<>();
                            for (int i = 0 ;i<list.size();i++){
                                booksTable = list.get(i);
                                BookselectR bookselectR = new BookselectR();
                                bookselectR.setBookid(booksTable.getBookid());
                                bookselectR.setTitle(booksTable.getTitle());
                                bookselectR.setAuthor(booksTable.getAuthor());
                                bookselectR.setCategory(booksTable.getCategory());
                                bookselectR.setTotalcopies(booksTable.getTotalcopies());
                                bookselectR.setAvailablecopies(booksTable.getAvailablecopies());
                                list1.add(bookselectR);
                            }
                            return Result.success(list1, list1.size());
                        } else {
                            return Result.fail("没有找到符合的图书信息");
                        }
                    } else {
                        CategoriesTable categoriesTable = categoriesTableService.queryByCategoryName(bookselect.getCategory());
                        if (categoriesTable != null) {
                            List<BooksTable> list = booksTableService.selectbookList(bookselect.getTitle(), bookselect.getAuthor(), bookselect.getCategory());
                            if (!list.isEmpty()) {
                                ArrayList<BookselectR> list1 = new ArrayList<>();
                                for (int i = 0 ;i<list.size();i++){
                                    booksTable = list.get(i);
                                   BookselectR bookselectR = new BookselectR();
                                    bookselectR.setBookid(booksTable.getBookid());
                                    bookselectR.setTitle(booksTable.getTitle());
                                    bookselectR.setAuthor(booksTable.getAuthor());
                                    bookselectR.setCategory(booksTable.getCategory());
                                    bookselectR.setTotalcopies(booksTable.getTotalcopies());
                                    list1.add(bookselectR);
                                }
                                return Result.success(list1, list1.size());
                            } else {
                                return Result.fail("没有找到符合的图书信息");
                            }
                        } else {
                            return Result.fail("没有找到基于图书类别");
                        }
                    }

                } else {
                    return Result.fail("该学号不存在");
                }
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }




    @PostMapping("/booklists")
    @ApiOperation(value = "图书列表", notes = "图书总列表")
    public Result<List<BookselectR>> bookList(@RequestBody Booklist booklist) {
        Map<String, Object> parsed = JwtUtil.tokenPre(booklist.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                AccountTable accountTable = accountTableService.queryByAccount((String) account);
                if (accountTable != null) {
                    List<BooksTable> list = booksTableService.list();
                    if (!list.isEmpty()) {
                        ArrayList<BookselectR> list1 = new ArrayList<>();
                        for (int i = 0 ;i<list.size();i++){
                            booksTable = list.get(i);
                            BookselectR bookselectR = new BookselectR();
                            bookselectR.setBookid(booksTable.getBookid());
                            bookselectR.setTitle(booksTable.getTitle());
                            bookselectR.setAuthor(booksTable.getAuthor());
                            bookselectR.setCategory(booksTable.getCategory());
                            bookselectR.setTotalcopies(booksTable.getTotalcopies());
                            bookselectR.setAvailablecopies(booksTable.getAvailablecopies());
                            list1.add(bookselectR);
                        }
                        return Result.success(list1, list1.size());
                    } else {
                        return Result.fail("暂时没有数据");
                    }
                } else {
                    return Result.fail("该学号不存在");
                }
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }


}



